import redis


class RedisClient(object):
    def __init__(self, host='103.7.142.131', port=6379, password='950218'):
        """
        初始化Redis连接
        :param host: Redis 地址
        :param port: Redis 端口
        :param password: Redis 密码
        """
        self.pool = redis.ConnectionPool(**{
            'host': host,
            'port': port,
            'db': 0,
            'password': password,
            'decode_responses': True
        })
        self.db = redis.StrictRedis(connection_pool=self.pool)
        self.key = 'report_dev'

    def reConnect(self):
        try:
            self.db.ping()
        except:
            self.db = redis.StrictRedis(connection_pool=self.pool)

    @property
    def get_order_number(self):
        """git
        获取任务
        """
        return self.db.lpop(self.key)

    @property
    def get_aigc_order_number(self):
        """git
        获取任务
        """
        return self.db.lpop('aigc_task')

    def push_order_number(self, order_number):
        """git
        获取任务
        """
        return self.db.rpush(self.key, order_number)

    def setGZHToken(self, token, expire):
        self.db.set('gzh_access_token', token, expire - 120)

    def GetGZHToken(self):
        token = self.db.get('gzh_access_token')
        if token:
            return token
        else:
            return None
